package com.Leetcode;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.PriorityQueue;
import java.util.Queue;

public class Index1996numberOfWeakCharacters {
	Queue<Integer> week = new PriorityQueue<>((o1,o2) -> o1-o2);
	public static void main(String[] args) {
		int[][] properties = new int[][]{{7,9},{10,7},{6,9},{10,4},{7,5},{7,10}};
		Index1996numberOfWeakCharacters a = new Index1996numberOfWeakCharacters();
		System.out.println(a.numberOfWeakCharacters(properties));
	}
	
	public int numberOfWeakCharacters(int[][] properties) {
	   int ans = 0;
	   Arrays.sort(properties, (o1, o2) -> {
           return o1[0] == o2[0] ? (o2[1] - o1[1]) : (o1[0] - o2[0]);
       });
       for (int[] p : properties) {
    	   while(!week.isEmpty() && week.peek() < p[1]) {
    		   ans++;
    		   week.poll();
    	   }
    	   week.offer(p[1]);
       }
       return ans;
    }
	
	public int numberOfWeakCharacters2(int[][] properties) {
        Arrays.sort(properties, (o1, o2) -> {
            return o1[0] == o2[0] ? (o1[1] - o2[1]) : (o2[0] - o1[0]);
        });
        int maxDef = 0;
        int ans = 0;
        for (int[] p : properties) {
            if (p[1] < maxDef) {
                ans++;
            } else {
                maxDef = p[1];
            }
        }
        return ans;
    }
}

